// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::cipher;

@test fn cbc_encrypt_decrypt() void = {
	const key: [_]u8 = [
		0xde, 0x05, 0x73, 0x76, 0xbf, 0x1b, 0x0c, 0xd1,
		0x26, 0x66, 0xa9, 0x63, 0x30, 0xce, 0x1c, 0xde,
	];

	const iv: [_]u8 = [
		0x2c, 0x80, 0x9e, 0x1e, 0xc3, 0x02, 0x28, 0xae,
		0x0a, 0x19, 0xea, 0xef, 0x73, 0x6c, 0x9a, 0xbb,
	];

	const plain: [_]u8 = [
		0x50, 0xe6, 0xd6, 0x87, 0xeb, 0xb4, 0x65, 0x9c,
		0xfc, 0x70, 0x7e, 0x00, 0xe6, 0xd6, 0xef, 0x23,
		0xd6, 0x11, 0xca, 0x11, 0x08, 0x2a, 0xde, 0x07,
		0xf2, 0xe4, 0x09, 0x17, 0x5a, 0xac, 0xdf, 0x6c,
		0x9b, 0x08, 0x82, 0x92, 0x7e, 0x2c, 0xbc, 0xb7,
		0x74, 0xe0, 0xe8, 0x7b, 0xe5, 0x72, 0x86, 0x6f,
		0x92, 0x37, 0x85, 0xb0, 0x5e, 0x53, 0xd0, 0xcb,
		0x42, 0x70, 0x3d, 0x49, 0x6a, 0x48, 0x47, 0x36,
	];

	const cipher: [_]u8 = [
		0xb4, 0x6b, 0x0b, 0x36, 0xe1, 0xd7, 0x84, 0x1a,
		0xf4, 0x3e, 0x74, 0x29, 0x72, 0x9c, 0x60, 0x60,
		0xe1, 0x8a, 0xfc, 0x87, 0x3b, 0xe3, 0x18, 0x89,
		0x8c, 0x5b, 0x5f, 0x12, 0xe6, 0x71, 0x0d, 0x7e,
		0xf3, 0xfd, 0xa6, 0x82, 0x4e, 0xa8, 0x86, 0x9f,
		0xaf, 0x60, 0xe9, 0x16, 0x05, 0xda, 0xe1, 0xa5,
		0xaa, 0xc0, 0x57, 0x20, 0xf8, 0xbf, 0x17, 0x37,
		0x74, 0x8d, 0xd5, 0x3d, 0xf8, 0x0c, 0x97, 0x05,
	];

	let result: [64]u8 = [0...];
	let buf: [CBC_BUFSZ]u8 = [0...];

	let b = ct64();
	ct64_init(&b, key);
	defer cipher::finish(&b);

	let cbc = cipher::cbc_encryptor(&b, iv[..], buf[..]);

	cipher::cbc_encrypt(&cbc, result, plain);
	assert(bytes::equal(cipher, result));

	let cbcd = cipher::cbc_decryptor(&b, iv[..], buf[..]);

	cipher::cbc_decrypt(&cbcd, result, cipher);
	assert(bytes::equal(plain, result));
};

@test fn cbc_encrypt_decrypt_in_place() void = {
	const key: [_]u8 = [
		0xde, 0x05, 0x73, 0x76, 0xbf, 0x1b, 0x0c, 0xd1,
		0x26, 0x66, 0xa9, 0x63, 0x30, 0xce, 0x1c, 0xde,
	];

	const iv: [_]u8 = [
		0x2c, 0x80, 0x9e, 0x1e, 0xc3, 0x02, 0x28, 0xae,
		0x0a, 0x19, 0xea, 0xef, 0x73, 0x6c, 0x9a, 0xbb,
	];

	const plain: [_]u8 = [
		0x50, 0xe6, 0xd6, 0x87, 0xeb, 0xb4, 0x65, 0x9c,
		0xfc, 0x70, 0x7e, 0x00, 0xe6, 0xd6, 0xef, 0x23,
		0xd6, 0x11, 0xca, 0x11, 0x08, 0x2a, 0xde, 0x07,
		0xf2, 0xe4, 0x09, 0x17, 0x5a, 0xac, 0xdf, 0x6c,
		0x9b, 0x08, 0x82, 0x92, 0x7e, 0x2c, 0xbc, 0xb7,
		0x74, 0xe0, 0xe8, 0x7b, 0xe5, 0x72, 0x86, 0x6f,
		0x92, 0x37, 0x85, 0xb0, 0x5e, 0x53, 0xd0, 0xcb,
		0x42, 0x70, 0x3d, 0x49, 0x6a, 0x48, 0x47, 0x36,
	];

	const cipher: [_]u8 = [
		0xb4, 0x6b, 0x0b, 0x36, 0xe1, 0xd7, 0x84, 0x1a,
		0xf4, 0x3e, 0x74, 0x29, 0x72, 0x9c, 0x60, 0x60,
		0xe1, 0x8a, 0xfc, 0x87, 0x3b, 0xe3, 0x18, 0x89,
		0x8c, 0x5b, 0x5f, 0x12, 0xe6, 0x71, 0x0d, 0x7e,
		0xf3, 0xfd, 0xa6, 0x82, 0x4e, 0xa8, 0x86, 0x9f,
		0xaf, 0x60, 0xe9, 0x16, 0x05, 0xda, 0xe1, 0xa5,
		0xaa, 0xc0, 0x57, 0x20, 0xf8, 0xbf, 0x17, 0x37,
		0x74, 0x8d, 0xd5, 0x3d, 0xf8, 0x0c, 0x97, 0x05,
	];

	let result: [64]u8 = plain;
	let buf: [CBC_BUFSZ]u8 = [0...];

	let b = ct64();
	ct64_init(&b, key);
	defer cipher::finish(&b);

	let cbc = cipher::cbc_encryptor(&b, iv[..], buf[..]);

	cipher::cbc_encrypt(&cbc, result, result);
	assert(bytes::equal(cipher, result));

	let cbcd = cipher::cbc_decryptor(&b, iv[..], buf[..]);

	cipher::cbc_decrypt(&cbcd, result, result);
	assert(bytes::equal(plain, result));
};
